﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Presentation;

namespace Presentation
{
    public class MainViewModel : INotifyPropertyChanged
    {
        ILogger log;
        private LengthConverter lengthConverter = new LengthConverter();
        public ObservableCollection<string> FromTypes { get; set; }
        public ObservableCollection<string> ToTypes { get; set; }
        string fromType;
        public string FromType
        {
            get
            {
                return fromType;
            }
            set
            {
                fromType = value;
                RaisePropertyChanged("FromType");
                UpdateToValue();
            }
        }
        double fromValue;
        public double FromValue
        {
            get
            {
                return fromValue;
            }
            set
            {
                fromValue = value;
                RaisePropertyChanged("FromValue");
                UpdateToValue();
            }
        }
        string toType;
        public string ToType
        {
            get
            {
                return toType;
            }
            set
            {
                toType = value;
                RaisePropertyChanged("ToType");
                UpdateToValue();
            }
        }
        private double toValue;
        public double ToValue
        {
            get
            {
                return toValue;
            }
            set
            {
                toValue = value;
                RaisePropertyChanged("ToValue");
            }
        }
        public string Log
        {
            get
            {
                return log.Read();
            }
        }
        private void UpdateToValue()
        {
            double result = 0.0;
            try
            {
                result = lengthConverter.Convert(FromValue, FromType, ToType);
                log.Write("FromValue=" + FromValue.ToString() + 
                    ";FromType=" + FromType +
                    ";ToType=" + ToType +
                    ";Result=" + result.ToString());
               RaisePropertyChanged("Log");
            }
            catch (System.Exception ex)
            {
            }
            ToValue = result;
        }
        public MainViewModel(ILogger log)
        {
            this.log = log;
            FromValue = 1.0;
            FromTypes = new ObservableCollection<string>(lengthConverter.MultiplyToMeter.Keys);
            ToTypes = new ObservableCollection<string>(lengthConverter.MultiplyToMeter.Keys);
            FromType = FromTypes[0];
            ToType = ToTypes[0];
        }

        #region INotifyPropertyChanged Members
        void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
